package com.aaa.aspectFPA;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import com.aaa.entity.system.Userinfo;
import lombok.extern.java.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author ：ludi（206913808@qq.com）
 * @date ：Created in 2022/9/6 16:44
 * @description：
 * @modified By：
 * @version:
 */
@Component
@Aspect
@EnableAspectJAutoProxy
@Log
public class MyInsertUpdateAspectFPA {
    @Pointcut("@annotation(com.aaa.aspectFPA.InsertAnnFPA)")
    public void xxx() {
    }

    @Pointcut("@annotation(com.aaa.aspectFPA.UpdateAnnFPA)")
    public void yyy() {
    }

    @Before("xxx()")
    public void insertEnhance(JoinPoint joinPoint) {
        //方法名
        String name = joinPoint.getSignature().getName();
        //参数
        Object[] args = joinPoint.getArgs();

        try {
            if (ObjectUtil.isNotEmpty(args)) {
                //第一个参数
                Object arg = args[0];
                //糊涂工具 对上面的参数进行增强处理,让argFirst对象进行方法setCreateTime的自动执行
                ReflectUtil.invoke(arg, "setCreate_time", new Date());
                /**
                 * xxxx
                 */
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = requestAttributes.getRequest();
                Userinfo user = (Userinfo) request.getSession().getAttribute("user");
                String username = user.getUsername();
                ReflectUtil.invoke(arg, "setCreate_by", username);
            }

        }catch(Exception e){
            log.info("实体类中没有create相关字段");
        }
    }

    @Before("yyy()")
    public void updateEnhance(JoinPoint joinPoint) {
        //方法名
        String name = joinPoint.getSignature().getName();
        //参数
        Object[] args = joinPoint.getArgs();
        try {
            if (ObjectUtil.isNotEmpty(args)) {
                //第一个参数
                Object arg = args[0];
                //糊涂工具 对上面的参数进行增强处理,让argFirst对象进行方法setUpdateTime的自动执行
                ReflectUtil.invoke(arg, "setUpdate_time", new Date());
                /**
                 * xxxx
                 */
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = requestAttributes.getRequest();
                Userinfo user = (Userinfo) request.getSession().getAttribute("user");
                String username = user.getUsername();
                ReflectUtil.invoke(arg, "setUpdate_by", username);
            }
        }catch (Exception e){
            log.info("实体类中没有update相关字段");
        }

    }
}